import java.util.Scanner;

/**
 * Author: 王俊超
 * Time: 2016-05-06 17:52
 * CSDN: http://blog.csdn.net/derrantcm
 * Github: https://github.com/Wang-Jun-Chao
 * Declaration: All Rights Reserved !!!
 */
public class Main {

    public static void main(String[] args) {
        String str = "we are happy";
        replaceAllBlank(str);
    }

    /**
     * 获取字符串空格的个数
     * @return
     */
    public static int getBlankNum(String str){
        int count=0;
        for(int i=0;i<str.length();i++){
            String c = String.valueOf(str.charAt(i));
            if(c.equals(" ")){
                count++;
            }
        }
        return count;
    }

    /**
     * 打印输出二维数组
     * @param array
     */
    public static void printArray(char[] array){
        for(char c : array){
            System.out.print(c);
        }
        System.out.println();
    }

    /**
     * 替换掉所有的空格
     * @param str
     */
    public static void replaceAllBlank(String str){
        if(str==null || str=="" ||str.length()==0){
            return ;
        }
        int oldLength = str.length();
        int newLength = oldLength + getBlankNum(str) * 2;
        char[] newArray = new char[newLength];
        //拷贝数组，统一在newArray上操作
        System.arraycopy(str.toCharArray(),0,newArray,0,oldLength);
        int oldPoint = oldLength-1;
        int newPoint = newLength-1;
        System.out.println("========旧数组===========");
        printArray(str.toCharArray());
        while(oldPoint>=0 && newPoint>=0 && oldPoint !=newPoint){
            if(newArray[oldPoint]==' '){
                newArray[newPoint--] = '0';
                newArray[newPoint--] = '2';
                newArray[newPoint--] = '%';
            }else{
                newArray[newPoint--] = newArray[oldPoint];
            }
            oldPoint--;
        }
        System.out.println("==========新数组===========");
        printArray(newArray);
    }
}
